/*
  author Sylvain Bertrand <digital.ragnarok@gmail.com>
  Protected by GNU Affero GPL v3 with some exceptions.
  See README at root of alga tree.
*/
#include <linux/module.h>
#include <linux/slab.h>

#include <alga/alga.h>
#include <alga/pixel_fmts.h>

int alga_pixel_fmts_init(enum alga_pixel_fmt **fmts)
{
	*fmts = kzalloc(sizeof(**fmts), GFP_KERNEL);
	if (*fmts == NULL)
		return -ALGA_ERR;
	*fmts[0] = ALGA_PIXEL_FMT_INVALID;
	return 0;
}
EXPORT_SYMBOL_GPL(alga_pixel_fmts_init);

int alga_pixel_fmts_add(enum alga_pixel_fmt fmt, enum alga_pixel_fmt **fmts)
{
	unsigned i;
	enum alga_pixel_fmt *new;

	i = 0;
	while ((*fmts)[i] != ALGA_PIXEL_FMT_INVALID) /* get to the terminator */
		++i;

	new = krealloc(*fmts, sizeof(fmt) * (i + 2), GFP_KERNEL);
	if (new == NULL)
		return -ALGA_ERR;
	*fmts = new;

	(*fmts)[i + 1] = ALGA_PIXEL_FMT_INVALID; /* terminator */
	(*fmts)[i] = fmt; /* insert the new pixel format at the end */
	return 0;
}
EXPORT_SYMBOL_GPL(alga_pixel_fmts_add);
